package com.mxpio.erp.plan.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.plan.entity.MpsLine;
import com.mxpio.erp.common.plan.service.MpsLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "MpsLineController", description = "主计划清单接口")
@RestController
@RequestMapping("/erp/plan/mps/line/")
public class MpsLineController {

	@Autowired
	private MpsLineService mpsLineService;

	@GetMapping("list/{versionCode}")
	@Operation(summary = "主计划清单列表", description = "获取主计划清单列表（无分页）", method = "GET")
	public Result<List<MpsLine>> list(@Parameter(description = "查询参数") Criteria criteria,
			@PathVariable(name = "versionCode") String versionCode)
			throws UnsupportedEncodingException {
		criteria.addCriterion("versionCode", Operator.EQ, versionCode);
		List<MpsLine> mpsLine = mpsLineService.list(MpsLine.class, criteria);
		mpsLineService.associatedTransientFields(mpsLine);
		return Result.OK(mpsLine);
	}
	
	@GetMapping("page/{versionCode}")
	@Operation(summary = "主计划清单列表", description = "获取主计划清单列表（分页）", method = "GET")
	public Result<Page<MpsLine>> page(@Parameter(description = "查询参数") Criteria criteria,
			@PathVariable(name = "versionCode") String versionCode,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("versionCode", Operator.EQ, versionCode);
		Page<MpsLine> mpsLine = mpsLineService.listPage(MpsLine.class, page, criteria);
		mpsLineService.associatedTransientFields(mpsLine.getContent());
		return Result.OK(mpsLine);
	}
	
	

	/*@GetMapping("list/{id}")
	@Operation(summary = "根据id获取主计划清单", description = "根据id获取主计划清单", method = "GET")
	public Result<MpsLine> getById(
			@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id) {
		MpsLine mpsLine = mpsLineService.getById(MpsLine.class, id);
		return Result.OK(mpsLine);
	}*/

	@RequestMapping("save")
	@Operation(summary = "保存主计划清单", description = "保存主计划清单")
	public Result<List<MpsLine>> save(@RequestBody List<MpsLine> mpsLines) {
		mpsLineService.save(mpsLines);
		return Result.OK(mpsLines);
	}

	@DeleteMapping("remove/{ids}")
	@Operation(summary = "根据ids删除主计划清单", description = "删除主计划清单", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<MpsLine> remove(
			@Parameter(description = "id，多个单号用英文逗号分隔") @PathVariable(name = "ids", required = true) String ids) {
		String[] id = ids.split(",");
		for (String key : id) {
			mpsLineService.delete(MpsLine.class, key);
		}
		return Result.OK();
	}
}
